package com.xl333.mysql.service;

import com.xl333.mysql.dao.ColumnDao;
import com.xl333.mysql.dao.TableDao;
import com.xl333.mysql.model.Column;
import com.xl333.mysql.model.Table;
import com.xl333.util.LetterUtil;
import com.xl333.util.MysqlDataTypeUtil;

import java.util.List;

/**
 * @author 七月的狮子
 * @date 2020/5/18
 */
public class TableService {

    private static TableService instance = null;

    private TableService(){

    }

    public static TableService getInstance(){
        if(instance == null){
            instance = new TableService();
        }
        return instance;
    }

    public List<Table> getTableInfo(String schema, String [] tableName){
        List<Table> tableList = TableDao.getInstance().queryBySchemaAndName(schema, tableName);
        for(Table table:tableList){
            table.setHumpName(LetterUtil.firstUpperHump(table.getName()));
            table.setSecondHumpName(LetterUtil.secondUpperHump(table.getName()));
            List<Column> columnList = ColumnDao.getInstance().queryBySchemaAndTable(schema, table.getName());
            for(Column column:columnList){
                column.setHumpName(LetterUtil.secondUpperHump(column.getName()));
                column.setJavaDataType(MysqlDataTypeUtil.getJavaType(column.getDataType()));
                column.setDataType(MysqlDataTypeUtil.getJdbcType(column.getDataType()));
                if(!table.getExistsBigDecimal() && column.getJavaDataType().equals("BigDecimal")){
                    table.setExistsBigDecimal(true);
                }
            }

            table.setColumnList(columnList);
        }
        return tableList;
    }

}
